package cn.kongwg.servlet;

import cn.kongwg.pojo.Person;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author kongwg
 * @create 2021-01-31 17:37
 */
@WebServlet("/ajaxServlet")
public class AjaxServlet extends BaseServlet {

    protected void ajax(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("ajax方法调用了");
        Person person = new Person(1, "kongwg");
        Gson gson = new Gson();
        String json = gson.toJson(person);
        resp.getWriter().write(json);
    }

    protected void get(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("get方法调用了");
        Person person = new Person(1, "kongwg");
        Gson gson = new Gson();
        String json = gson.toJson(person);
        resp.getWriter().write(json);
    }

    protected void post(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("post方法调用了");
        Person person = new Person(1, "kongwg");
        Gson gson = new Gson();
        String json = gson.toJson(person);
        resp.getWriter().write(json);
    }

    protected void getJson(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("getJson 方法调用了");
        Person person = new Person(1, "kongwg");
        Gson gson = new Gson();
        String json = gson.toJson(person);
        resp.getWriter().write(json);
    }

    protected void getJsonSerialize(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("getjsonSerialize方法调用了");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username+"---"+password);
        Person person = new Person(1, "kongwg");
        Gson gson = new Gson();
        String json = gson.toJson(person);
        resp.getWriter().write(json);
    }
}
